package com.godliu.file.demo.utils;

import com.godliu.file.demo.pojo.BaseFile;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

@Slf4j
public class FileUtils {
    /**
     * 获取文件后缀名  .jpg
     *
     * @param file
     * @return
     */
    public static String getExtension(File file) {
        if (file == null || !file.exists()) {
            log.error("file is null or not exists");
            return null;
        }
        try {
            String fileRealPath = file.getCanonicalPath();
            String fileextension = fileRealPath.substring(fileRealPath.lastIndexOf(".")).toLowerCase();
            return fileextension;
        } catch (Exception e) {
            log.error("获取文件后缀名", e);
        }
        return null;
    }

    /**
     * 根据文件和BaseFile，获取目标文件路径，同时补充BaseFile信息
     *
     * @param file 源文件
     * @param bf   BaseFile对象
     *             如果为null，则在原图压缩
     *             如果bf对象的newPath值为空，则压缩后的图片名为原图片名加“_”前缀
     *             如果bf对象指定了newPath值，则压缩后的图片名为指定的图片名
     * @return 目标文件路径
     */
    public static String getOutFilePath(File file, BaseFile bf) {
        String outFilePath = "";
        try {
            String oldfilePath = file.getCanonicalPath();
            String sepa = File.separator;
            String oldfullPath = "";
            String oldfilename = "";
            if (oldfilePath.indexOf(sepa) > -1) {
                int index = oldfilePath.lastIndexOf(sepa);
                oldfullPath = oldfilePath.substring(0, index);
                oldfilename = oldfilePath.substring(index + 1);
            }

            String fullPath = "";
            String realfilename = "";
            if (bf == null) {
                outFilePath = file.getCanonicalPath();
            } else if (!StringUtils.isNullOrEmpty(bf.getNewPath())) {
                String fileRealPath = bf.getNewPath();
                if (fileRealPath.indexOf(sepa) > -1) {
                    int index = fileRealPath.lastIndexOf(sepa);
                    fullPath = fileRealPath.substring(0, index);
                    realfilename = fileRealPath.substring(index + 1);
                }
                outFilePath = bf.getNewPath();
                bf.setNewName(realfilename);
                bf.setNewPath(outFilePath);
                bf.setOldName(oldfilename);
            } else {
                String fileRealPath = file.getCanonicalPath();
                if (fileRealPath.indexOf(sepa) > -1) {
                    int index = fileRealPath.lastIndexOf(sepa);
                    fullPath = fileRealPath.substring(0, index);
                    realfilename = fileRealPath.substring(index + 1);
                }
                realfilename = "_" + realfilename;
                outFilePath = fullPath + "/" + realfilename;
                bf.setNewName(realfilename);
                bf.setNewPath(outFilePath);
                bf.setOldName(oldfilename);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
        return outFilePath;
    }
}
